<?php namespace Hdphp\Libs;

class App
{
	public static function run()
	{
		//初始化
		self::init();
		//注册一个自动载入的函数
		spl_autoload_register(array(__CLASS__,"auto"));
		//执行应用
		self::appRun();
	}
	//初始化
	private static function init()
	{
		//模块处理
		$m = isset($_GET['m']) ? $_GET['m'] :"Home";
		define("MODULE", $m);
		//控制器处理
		$c = isset($_GET['c']) ?$_GET['c'] :"Index";
		define("CONTROLLER", $c);
		//方法处理
		$a = isset($_GET['a']) ?$_GET['a'] :"index";
		define("ACTION", $a);
		//加载配置项Hdphp里
		$hdconfig = include "Hdphp/config.php";
		C($hdconfig);
		//加载Home里边的控制器
		$usercofig = include "App/Home/Config/config.php";
		C($usercofig);
		//开启session
		session_id() || session_start();
	} 
	//实例化一个未找见的类自动触发，会把类名传进来
	private static function auto($cName)
	{
		if(substr($cName, -10)=="Controller"){
			//如果是控制器，$cName现在是这样的：Home\Controller\IndexController
			$path = "App/" .str_replace("\\","/", $cName) . ".php";
			require $path;
		}else{
			//如果是工具类，$cName是这样的：Hdphp\Tool\Model
			$path =str_replace("\\", "/", $cName) . ".php";
			require $path;
		}
		
	}
	
	
	//执行应用
	private static function appRun()
	{
		$m = MODULE;//模块
		$c = CONTROLLER;//控制器
		$a = ACTION;//方法
		header("Content-type:text/html;charset=utf8");
		//实例化一个未找见的类   实例化\Home\Cotroller\IndexController
		$path = "\\{$m}\Controller\\{$c}Controller";
		$obj = new $path;
		$obj->$a();
	}
	

}





